<?php
class APi{
	static function zhuce($username,$phone,$email,$age,$sex,$password){
		$arr=[];
		if($username && $phone && $email&& $age && $sex && $password){
	//通过PDO连接MYSQL
    $db = new PDO("mysql:host=127.0.0.1;dbname=scholl;charset=utf8","root","123456");
	$sql="insert into stu (name,phone,pasword,age,sex,email)values(:username,:phone,:password,:age,:sex,:email)";
	$sth=$db->prepare($sql);
	$sth->bindParam(':username', $username, PDO::PARAM_STR);
	$sth->bindParam(':age', $age, PDO::PARAM_INT);
	$sth->bindParam(':sex', $sex, PDO::PARAM_STR);
	$sth->bindParam(':email', $email, PDO::PARAM_STR);
	$sth->bindParam(':phone', $phone, PDO::PARAM_STR);
	$password=md5($password);
	$sth->bindParam(':password', $password, PDO::PARAM_STR);
	$ret=$sth->execute();
	if($ret==1){
		$arr["error"]=0;
		$arr["message"]="Success";
	}else{
		$arr["error"]=103;
		$arr["message"]="注册失败";
	}
	header("Content-Type:text/json");
    echo json_encode($arr);
		}
	}
	static function denglu($username,$password){
	    $arr=[];
		if ($username && $password) {
	    $db = new PDO("mysql:host=127.0.0.1;dbname=scholl;charset=utf8", "root", "123456");
	    $sql = "select count(*) from stu where name=:username and pasword=:password";
	    //预处理
	    $sth = $db -> prepare($sql);
	    $sth -> bindParam(":username", $username);
	    $password = md5($password);
	    $sth -> bindParam(":password", $password);
	    $sth -> execute();
	    $ret = $sth -> fetchAll(PDO::FETCH_NUM);
	    if ($ret[0][0] == 1) {
	    $arr["error"]=0;
		$arr["message"]="Success";
	     } else {
		$arr["error"]=101;
		$arr["message"]="用户名高或密码错误";
	   }
	   
      }else{
      	$arr["error"]=102;
		$arr["message"]="用户名或密码不能为空";
      } 
      header("Content-Type:text/json");
	  echo json_encode($arr);
	}
}
$type=$_GET["type"];
if($type=="denglu"){
	$username = $_POST["username"];
    $password = $_POST["password"];
	APi::denglu($username,$password);
}else if($type=="zhuce"){
	$username = $_POST["username"];
    $password = $_POST["password"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $sex = $_POST["sex"];
    $age = $_POST["age"];
	APi::zhuce($username,$phone,$email,$age,$sex,$password);
}
?>